﻿Imports System.Drawing
Imports System.Windows.Forms

Public Class Mover
    Private sp As Point
    Private M_Owner As Control
    Private M_Edit As Boolean = True
    Public Property Owner() As Control
        Get
            Return M_Owner
        End Get
        Set(ByVal value As Control)
            M_Owner = value
        End Set
    End Property

    Public Sub Connect(ByVal Owner As Control)
        M_Owner = Owner
        DoConnect()
    End Sub
    Public Sub AddControl(ByVal Ctrl As Control)
        AddHandler Ctrl.MouseDown, AddressOf MouseDown
        AddHandler Ctrl.MouseMove, AddressOf MouseMove
        AddHandler Ctrl.MouseUp, AddressOf MouseUp
        Dim M_Con As New ContextMenuStrip
        Dim DC As New ToolStripMenuItem("Remove")
        M_Con.Items.Add(DC)
        AddHandler Ctrl.Click, AddressOf removeme
    End Sub
    Public Sub Connect()
        DoConnect()
    End Sub
    Private Sub DoConnect()
        If M_Owner IsNot Nothing Then
            AddHandler M_Owner.MouseDown, AddressOf MouseDown
            AddHandler M_Owner.MouseMove, AddressOf MouseMove
            AddHandler M_Owner.MouseUp, AddressOf MouseUp
            Dim M_Con As New ContextMenuStrip
            Dim DC As New ToolStripMenuItem("Remove")
            M_Con.Items.Add(DC)
            AddHandler DC.Click, AddressOf removeme
            M_Owner.ContextMenuStrip = M_Con
            If M_Owner.GetType.GetInterface("I_EditMode") IsNot Nothing Then
                'MsgBox("I_Edit")
            Else
                If TypeOf M_Owner.Parent Is DiagramPanel Then
                    Dim Diag As DiagramPanel = M_Owner.Parent
                    AddHandler Diag.ModeChanged, AddressOf ModeChanged
                Else
                    'MsgBox("Control Error")
                End If
            End If
        End If
    End Sub
    Private Sub ModeChanged(ByVal Mode As DiagramPanel.EditMode)
        If Mode = Diagram.EditMode.Locked Then
            M_Edit = False
        Else
            M_Edit = True
        End If
    End Sub
    Public Sub removeme()
        For Each C As Control In M_Owner.Controls
            If TypeOf C Is Socket Then
                Dim S As Socket = C
                If S.Connected = True Then
                    S.Disconnect()
                End If
            End If
        Next
        M_Owner.Dispose()

    End Sub
    Public Property EditMode() As Boolean
        Get
            Return M_Edit
        End Get
        Set(ByVal value As Boolean)
            M_Edit = value
        End Set
    End Property
    Private Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If M_Edit = True Then
            sp = Windows.Forms.Control.MousePosition
            M_Owner.SuspendLayout()
            For Each C As Control In M_Owner.Controls
                C.Visible = False
                'C.SuspendLayout()
            Next
        End If
    End Sub

    Private Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If M_Edit = True Then
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim P As Drawing.Point
                Dim NP As Drawing.Point
                NP.X = M_Owner.Left + (M_Owner.Width / 2)
                NP.Y = M_Owner.Top + (M_Owner.Height / 2)

                P = Windows.Forms.Control.MousePosition

                Dim X As Integer
                Dim Y As Integer

                X = P.X - sp.X
                Y = P.Y - sp.Y
                If Math.Abs(X) > 5 Or Math.Abs(Y) > 5 Then
                    M_Owner.Left = M_Owner.Left + X
                    M_Owner.Top = M_Owner.Top + Y

                    Dim V As Integer = (M_Owner.Top / 5)
                    M_Owner.Top = V * 5

                    V = (M_Owner.Left / 5)
                    M_Owner.Left = V * 5

                    If M_Owner.Left < 0 Then M_Owner.Left = 0
                    If M_Owner.Right > M_Owner.Parent.Width Then M_Owner.Left = M_Owner.Parent.Width - M_Owner.Width
                    If M_Owner.Top < 0 Then M_Owner.Top = 0
                    If M_Owner.Bottom > M_Owner.Parent.Height Then M_Owner.Top = M_Owner.Parent.Height - M_Owner.Height
                    sp = P
                    For Each C As Control In M_Owner.Controls
                        If TypeOf C Is Socket Then
                            Dim CX As Socket = C
                            CX.Reposition()
                        End If
                    Next
                End If
            End If
        End If
    End Sub
    Private Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If M_Edit = True Then
            M_Owner.ResumeLayout()
            For Each C As Control In M_Owner.Controls
                C.Visible = True
                'C.ResumeLayout()
            Next
        End If
    End Sub

End Class
